<?php
###############   COPYLEFT GPLv3 LICENSE   ###############
##
## JFX Version 0.2.9
## Website Management Software
## www.jfxcms.com
##
## Copyright 2009 GPLv3 - http://www.opensource.org/licenses/gpl-3.0.html
##
## Anthony Gallon
## oi_antz@hotmail.com
##
## Permission is hereby granted to any person having a copy of this software
## to freely use and modify as required so long as the copyright notices
## and branding remain intact.
##
## Full license details available at http://www.jfxcms.com/license
##
###############   COPYLEFT GPLv3 LICENSE   ###############

class JFX_Theme extends JFX_Module
{
    public $keyname = 'JFX_Theme';
    public $title = 'JFX Theme Base Class';
    public $version = '0.2.9';
    public $breadcrumbs = array();
	public $dirname;
	public $adminUrl;
	public $publicUrl;
	public $cssUrl;
	public $jsUrl;
	public $imageUrl;
	public $template_dir;
	public $templatesDir;
	
    public function __construct(){
        $CONFIG = JFX::registry('config');
        $SMARTY = JFX::registry('Smarty');

        $this->dirname = $CONFIG->themesDir.'/'.$this->keyname;
        $this->adminUrl = $CONFIG->baseUrl.$CONFIG->themeServerFolder.'/'.$this->keyname;
        $this->publicUrl = $CONFIG->baseUrl.$CONFIG->moduleServerFolder.'/'.$this->keyname;
        $this->cssUrl = $CONFIG->baseUrl.$CONFIG->moduleCssFolder.'/'.$this->keyname;
        $this->jsUrl = $CONFIG->baseUrl.$CONFIG->moduleJsFolder.'/'.$this->keyname;
        $this->imageUrl = $CONFIG->baseUrl.'/jfx/images/'.$this->keyname;
        //$this->imageUrl = $CONFIG->baseUrl.'/jfx/images/'.$this->keyname;
        $SMARTY->template_dir = $this->dirname.'/templates';
        $CONFIG->templatesDir = $this->dirname.'/templates';
        $this->templatesDir = $this->dirname.'/templates';
    }

    public function addBreadcrumb($url, $title, $icon=''){
        $count = count($this->breadcrumbs);
        $obj = new JFX_Theme_Breadcrumb($url, $title, $icon);
        $this->breadcrumbs[$count] = $obj;
    }

    public function getBreadcrumbs(){
        $THEME = JFX::registry('JFX_Theme');
        $bc = array();
        foreach($this->breadcrumbs as $k=>$v){
            if($k+1 == count($this->breadcrumbs)) $v->set('current', true);
            $bc[] = $THEME->parseBreadcrumbTemplate($v);
        }
        return '<div class="jfxt-breadcrumbs">'.$THEME->implodeBreadcrumbs($bc).'</div>';
    }

    public function implodeBreadcrumbs($bc){
         return '<p class="breadcrumbs">'.implode('<span class="jfxt-breadcrumb-spacer">/</span>', $bc).'</p>';
    }

    public function getMainMenu(){
        $menu = new JFX_Theme_Menu(0, 'root', 'Menu Root');
        $eParams = array(
            'menu' => $menu
        );
        JFX::hook('JFX_Theme.getMainMenu', $eParams);
        return $eParams['menu'];
    }

    public function parseMenuTemplate($menuObj){
        $content = '<li>'.$menuObj->get('title');
        if(count($menuObj->getItems())>0) $content .= '<ul>:|items|:</ul>';
        $content .= '</li>';
        return $content;
    }


    public static function isValidTheme($theme){
        $DB = JFX::registry('db');
        $CONFIG = JFX::registry('config');

        if(is_dir($CONFIG->themesDir.'/'.$theme) && file_exists($CONFIG->themesDir.'/'.$theme.'/'.$theme.'.theme.php')){
            require_once($CONFIG->themesDir.'/'.$theme.'/'.$theme.'.theme.php');
            $className = 'JFX_Theme_'.ucfirst(strtolower($theme));
            if(class_exists($className, false)){
                $obj = new $className;
                if($obj instanceof JFX_Theme) return true;
            }
        }
        return false;
    }

    public function install(){
        $CONFIG = JFX::registry('config');

        unlink_directory($CONFIG->publicDir.'/jfx/images/'.$this->keyname);
        copy_directory($this->dirname.'/images', $CONFIG->publicDir.'/jfx/images/'.$this->keyname);

    }

    public static function isInstalled($theme){

        if(!JFX_Theme::isValidTheme($theme)) return false;
        $DB = JFX::registry('db');
        $CONFIG = JFX::registry('config');
        $safeTheme = $DB->escape($theme);
        if($DB->countRows($CONFIG->dbprefix.'themes', "keyname = '{$safeTheme}'")==0) return false;
        else return true;
    }

	public static function getInstance($theme){
		if(!JFX_Theme::isValidTheme($theme)) return false;
        $className = 'JFX_Theme_'.ucfirst(strtolower($theme));
		return JFX::registry($className);
	}

    public static function newInstance($theme){
        if(!JFX_Theme::isValidTheme($theme)) return false;
        $className = 'JFX_Theme_'.ucfirst(strtolower($theme));
        $obj = new $className;
        return $obj;
    }

    public function serve($request){
        
        $request = explode('?', $request);
        $request = str_replace('..', '', $request[0]);
        $request = explode('/', trim($request, '/'));
        if(file_exists($this->dirname.'/actions/'.$request[0].'.php')) include($this->dirname.'/actions/'.$request[0].'.php');
    }

    public function getPageButtons($pageid){
        // get the toolbar buttons
        $CONFIG = JFX::registry('config');
        $installedModules = JFX_Module::getInstalledModules();
        $output = '';
        foreach($installedModules as $k=>$module){
            $buttons = $module['object']->getPageButtons($pageid);
            if(!is_array($buttons)) $buttons = array();
            foreach($buttons as $button){
                if(!$button instanceof JFX_Page_Button) continue;
                $output .= $this->renderPageButton($button);
            }
        }
        return $output;
        //return $this->renderPagesMenu($toolbarButtons);

    }


    public function getBlockActionButtons($blockid, $pageId){
        $modules = JFX_Module::getInstalledModules();
        $output = '';
        foreach($modules as $k=>$module){
            $buttons = $module['object']->getBlockActionButtons($blockid, $pageId);
			
            if(!is_array($buttons)) $buttons = array();
            foreach($buttons as $button){
                $output .= $this->renderBlockButton($button);
            }
        }
        return $output;
    }

	public function getPageActionButtons($pageid){
		$modules = JFX_Module::getInstalledModules();
        $output = '';
        foreach($modules as $k=>$module){
            $buttons = $module['object']->getPageActionButtons('jfxpage-'.$pageid, $pageid);
            if(!is_array($buttons)) $buttons = array();
            foreach($buttons as $button){
                $output .= $this->renderPageButton($button);
            }
        }
        return $output;
	}



    public function getBlockEditButtons($block){
        $modules = JFX_Module::getInstalledModules();
        $output = '';
        foreach($modules as $k=>$module){
            $buttons = $module['object']->getBlockEditButtons($block);
            if(!is_array($buttons)) $buttons = array();
            foreach($buttons as $button){
                $output .= $this->renderBlockButton($button);
            }
        }
        return $output;
    }

    public function renderPageButton($button){
		return $this->renderBlockButton($button);
    }

    public function renderBlockButton($button){
        if(false === $button instanceof JFX_Block_Button) return '';
        $atts = array();
        if(strlen($button->className) > 0){
            $atts['class'] = $button->className;
        }
        if(strlen($button->alternativeText) > 0){
            $atts['alt'] = $button->alternativeText;
        }
        if(strlen($button->imageUrl) > 0){
            $atts['src'] = $button->imageUrl;
        }
        if(strlen($button->title) > 0){
            $atts['title'] = $button->title;
        }
        if(strlen($button->id) > 0){
            $atts['id'] = $button->id;
        }
        if(strlen($button->moduleName) > 0 && strlen($button->moduleMethod) > 0){
			
			if(!empty($button->onclick)){
				$atts['onclick'] = $button->onclick;
			}elseif($button->onclick===null){
				$atts['onclick'] = '';
			}else{
				if(!$button->invisible){
					// for buttons that load content into the tabber
					$atts['onclick'] = "AdminBlocks.tabber.focusTab(".json_encode($button)."); JFXToolbarShowFunction() ";
					
				}else{
					// for buttons that perform silent background actions
					$rebuildPage = (int) $button->rebuildPage;
					$atts['onclick'] = $link = "AdminBlocks.tabber.triggerButton(".json_encode($button)."); ";
				}
			}
		}else{
			$atts['gota'] = 'echo blockId is '.$block->blockId.' instead of '.$block->id;
		}
        if(strlen($button->href) > 0){
            $atts['href'] = $button->href;
        }
        foreach($button->attributes as $k=>$v){
            if(strlen($v)>0){
                $atts[$k] = $v;
            }
        }

        $img = '<img ';
        foreach($atts as $attname=>$attvalue){
            $img .= htmlentities($attname).'="'.htmlentities($attvalue).'" ';
        }
        $img .= ' />';

        return $img;

    }

	
	
	
	


    public function getInstalledThemes(){
        $DB = JFX::registry('db');
        $CONFIG = JFX::registry('config');
        $themes = array();
        $installedThemes = $DB->fetchAll("SELECT * FROM {$CONFIG->dbprefix}themes ORDER BY sorting ASC");
        foreach($installedThemes as $k=>$v){
            $themes[$k] = $v;
            $themes[$k]['object'] = JFX::registry('JFX_Theme_'.ucfirst(strtolower($v['keyname'])));
        }
        return $themes;
    }




}


















